package site.kexing.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import site.kexing.config.MybaitsUtils;
import site.kexing.mapper.ProductMapper;
import site.kexing.mapper.Product_OrderMapper;
import site.kexing.mapper.Product_PlanMapper;
import site.kexing.mapper.Product_ScheduleMapper;
import site.kexing.pojo.Product_Order;
import site.kexing.pojo.Product_Plan;
import site.kexing.pojo.Product_Schedule;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class Product_OrderController {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private Product_OrderMapper product_orderMapper;
    @Autowired
    private Product_PlanMapper product_planMapper;
    @Autowired
    private Product_ScheduleMapper product_scheduleMapper;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    @RequestMapping("/selectallproductorder")
    public String selectAllProduct_Order(@RequestParam(defaultValue = "1") Integer currentPage, Model model){
        PageHelper.startPage(currentPage, 7);
        List<Product_Order> product_orders = product_orderMapper.selectAllProduct_Order();
        PageInfo<Product_Order> pageInfo = new PageInfo<Product_Order>(product_orders,7);
        model.addAttribute("pageInfo",pageInfo);
        List<Product_Order> list = pageInfo.getList();
        return "product_order";
    }

    @ResponseBody
    @RequestMapping("/selectordercountenddate")
    public String selectOrderCount_endDate(Integer order_id) {
        Product_Order product_order = product_orderMapper.selectProduct_OrderByID(order_id);
        String s = JSON.toJSONString(product_order);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/selectproductorderbyid")
    public String selectProduct_OrderByID(Integer id, Model model){
        //查询所有的产品ID
        List<Integer> productIds = productMapper.selectAllProductID();
        Product_Order product_order = product_orderMapper.selectProduct_OrderByID(id);
        model.addAttribute("productIds",productIds);
        model.addAttribute("product_order",product_order);
        return "product_orderUpdate";
    }

    @RequestMapping("/updateproductorder")
    public String updateProduct_Order(Product_Order product_order){
        product_order.setUpdate_time(new Date());
        int i = product_orderMapper.updateProduct_Order(product_order);
        if(product_order.getOrder_status() == 50){
            product_planMapper.updateProduct_plan_status(product_order.getId());
            //根据订单ID查询所有的计划ID
            List<Integer> pid = product_planMapper.selectAllPlanIDSByOrderID(product_order.getId());
            for(Integer plan_id : pid){
                int i1 = product_scheduleMapper.updateProduct_plan_status(plan_id);
            }
        }
        return "redirect:/selectallproductorder";
    }

    @RequestMapping("/deleteproductorder")
    public String deleteProduct_Order(Integer id){
        int i = product_orderMapper.deleteProduct_Order(id);
        return "redirect:/selectallproductorder";
    }

    @RequestMapping("/toproductorderinsert")
    public String toProduct_OrderInsert(Model model){
        //查询所有的产品ID
        List<Integer> productIds = productMapper.selectAllProductID();
        model.addAttribute("productIds",productIds);
        return "product_orderInsert";
    }

    @RequestMapping("/insertproductorder")
    public String insertProduct_Order(Product_Order product_order,Model model){
        product_order.setCreate_time(new Date());
        product_order.setUpdate_time(new Date());
        product_order.setOrder_seq(MybaitsUtils.getUUID());
        product_order.setOrder_source(10);
        int i = product_orderMapper.insertProduct_Order(product_order);
        if(product_order.getOrder_status() == 50){
            product_planMapper.updateProduct_plan_status(product_order.getId());
        }
        return "redirect:/selectallproductorder";
    }

    @RequestMapping("/selectproductorderbyproductid")
    public String selectProduct_OrderByProductID(@RequestParam(defaultValue = "1") Integer currentPage,String product_id,Model model){
        PageHelper.startPage(currentPage, 7);
        Integer id = 0;
        if(product_id==null || product_id==""){
            id = null;
        }else{
            id = Integer.parseInt(product_id);
        }
        List<Product_Order> product_orders = product_orderMapper.selectProduct_OrderByProductID(id);
        PageInfo<Product_Order> pageInfo = new PageInfo<Product_Order>(product_orders,7);
        model.addAttribute("pageInfo",pageInfo);
        return "product_order";
    }
}
